<template>
  <div class="ref-goods" v-for="item in topCategory?.children" :key="item.id">
    <div class="head">
      <h3>- {{ item.name }} -</h3>
      <p class="tag">温暖柔软，品质之选</p>
      <XtxMore />
    </div>
    <div class="body">
      <GoodsItem :goods="goods" v-for="goods in item.goods" :key="goods.id" />
    </div>
  </div>
</template>

<script>
import GoodsItem from "@/views/categroy/components/GoodsItem";
import { ref } from "vue";
import { getTopCategory } from "@/api/categroy";
import { useRoute, onBeforeRouteUpdate } from "vue-router";
export default {
  name: "CommandGoods",
  components: { GoodsItem },
  setup() {
    const route = useRoute();
    const { topCategory, getData } = useTopCategory();
    getData(route.params.id);
    return {
      topCategory,
    };
  },
};

function useTopCategory() {
  // 储存一级分类的数据
  const topCategory = ref();
  // 获取一级分类方法
  const getData = (id) => {
    getTopCategory(id).then((data) => {
      topCategory.value = data.result;
      console.log(topCategory.value);
    });
  };
  // 路由变化钩子                   to：跳转到的路由的信息
  onBeforeRouteUpdate((to) => getData(to.params.id));
  return {
    topCategory,
    getData,
  };
}
</script>

<style lang="less" scoped>
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
</style>
